#include "selecttime.h"
#include "ui_selecttime.h"

#include "configuredialog.h"
#include "ai_instant.h"

extern int trainTime;

SelectTime::SelectTime(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SelectTime)
{
    ui->setupUi(this);
    connect(ui->min1_radioButton,SIGNAL(toggled(bool)),this,SLOT(slot_RadioBtnChanged()));
    connect(ui->min5_radioButton,SIGNAL(toggled(bool)),this,SLOT(slot_RadioBtnChanged()));
    connect(ui->min10_radioButton,SIGNAL(toggled(bool)),this,SLOT(slot_RadioBtnChanged()));
    connect(ui->min15_radioButton,SIGNAL(toggled(bool)),this,SLOT(slot_RadioBtnChanged()));
    connect(ui->min20_radioButton,SIGNAL(toggled(bool)),this,SLOT(slot_RadioBtnChanged()));
}

void SelectTime::slot_RadioBtnChanged(){
    ui->OK_btn->setEnabled(true);
    ui->hour_spinBox->setValue(0);
    ui->minute_spinBox->setValue(0);
    radioTime = 600;
    if(ui->min1_radioButton->isChecked()) radioTime = 60;
    else if(ui->min5_radioButton->isChecked()) radioTime= 300;
    else if(ui->min10_radioButton->isChecked()) radioTime = 600;
    else if(ui->min15_radioButton->isChecked()) radioTime = 900;
    else if(ui->min20_radioButton->isChecked()) radioTime = 1200;
}

SelectTime::~SelectTime()
{
    delete ui;
}

void SelectTime::on_hour_dial_sliderMoved(int position)
{
    ui->OK_btn->setEnabled(true);
    ui->hour_spinBox->setValue(position);
}

void SelectTime::on_minute_dial_sliderMoved(int position)
{
    ui->OK_btn->setEnabled(true);
    ui->minute_spinBox->setValue(position);
}

void SelectTime::on_hour_spinBox_valueChanged(int arg1)
{
    ui->OK_btn->setEnabled(true);
    ui->hour_dial->setValue(arg1);
    spinTimeHour = arg1 * 3600;
}

void SelectTime::on_minute_spinBox_valueChanged(int arg1)
{
    ui->OK_btn->setEnabled(true);
    ui->minute_dial->setValue(arg1);
    spinTimeMinute = arg1 * 60;
}

void SelectTime::on_OK_btn_clicked()
{
    if(ui->hour_dial->value() == 0 && ui->minute_dial->value() == 0) trainTime = radioTime;
    else trainTime = spinTimeHour + spinTimeMinute;
    //创建设备以及初始化
    ConfigureDialog dialog;
    Ai_Instant *ai=new Ai_Instant;
//    int resultDialog = dialog.exec();
//    if (resultDialog==QDialog::Rejected){
//        ai->close();
//    }
//    else if (resultDialog==QDialog::Accepted){
//        ai->SetConfigureDialog(&dialog);
//        ai->SetConfigureParameter(dialog.GetConfigureParameter());
//        ai->Initialize();
//        ai->show();
//    }
//    this->close();
    ai->SetConfigureDialog(&dialog);
    ai->SetConfigureParameter(dialog.GetConfigureParameter());
    ai->Initialize();
    ai->show();
    this->close();
}
